import { Event, IProcessEnvironment, OperatingSystem } from '../../utils';

/* ---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

/**
 * 部分代码来源并修改自
 * https://github.com/microsoft/vscode/blob/1.55.0/src/vs/workbench/contrib/terminal/common/environmentVariable.ts
 * https://github.com/microsoft/vscode/blob/1.55.0/src/vs/platform/terminal/common/environmentVariable.ts
 */

// 环境变量集合接口
export interface IEnvironmentVariableCollection {
  readonly map: ReadonlyMap<string, IEnvironmentVariableMutator>; // 环境变量名到变更器的映射
}

// 带扩展标识的环境变量变更器接口
export interface IExtensionOwnedEnvironmentVariableMutator
  extends IEnvironmentVariableMutator {
  readonly extensionIdentifier: string; // 扩展唯一标识
}

// 合并环境变量集合的差异接口
export interface IMergedEnvironmentVariableCollectionDiff {
  added: ReadonlyMap<string, IExtensionOwnedEnvironmentVariableMutator[]>; // 新增
  changed: ReadonlyMap<string, IExtensionOwnedEnvironmentVariableMutator[]>; // 变更
  removed: ReadonlyMap<string, IExtensionOwnedEnvironmentVariableMutator[]>; // 移除
}

/**
 * 表示由多个集合合并而成的环境变量集合
 */
export interface IMergedEnvironmentVariableCollection {
  readonly map: ReadonlyMap<
    string,
    IExtensionOwnedEnvironmentVariableMutator[]
  >; // 合并后的映射

  /**
   * 应用此集合到进程环境变量
   * @param variableResolver 可选，变量解析函数
   */
  applyToProcessEnvironment(
    env: IProcessEnvironment,
    os: OperatingSystem,
    variableResolver?: (str: string) => PromiseLike<string>
  ): Promise<void>;

  /**
   * 生成与另一个集合的差异。如果集合相同则返回 undefined
   */
  diff(
    other: IMergedEnvironmentVariableCollection
  ): IMergedEnvironmentVariableCollectionDiff | undefined;
}

// 环境变量服务接口
export interface IEnvironmentVariableService {
  // 初始化环境变量集合
  initEnvironmentVariableCollections(): Promise<void>;

  /**
   * 获取所有扩展的环境变量集合
   */
  readonly collections: ReadonlyMap<string, IEnvironmentVariableCollection>;

  /**
   * 获取合并后的环境变量集合
   */
  readonly mergedCollection: IMergedEnvironmentVariableCollection | undefined;

  /**
   * 当扩展的环境变量集合变化时触发，事件参数为新的合并集合
   */
  onDidChangeCollections: Event<IMergedEnvironmentVariableCollection>;

  /**
   * 设置某扩展的环境变量集合
   */
  set(
    extensionIdentifier: string,
    collection: IEnvironmentVariableCollection
  ): void;

  /**
   * 删除某扩展的环境变量集合
   */
  delete(extensionIdentifier: string): void;
}

// 环境变量变更类型枚举
export enum EnvironmentVariableMutatorType {
  Replace = 1, // 替换
  Append = 2, // 追加
  Prepend = 3, // 前置
}

// 环境变量变更器选项
export interface EnvironmentVariableMutatorOptions {
  applyAtProcessCreation?: boolean; // 是否在进程创建时应用
}

// 环境变量变更器接口（带类型信息）
export interface IEnvironmentVariableMutator {
  readonly value: string;
  readonly type: EnvironmentVariableMutatorType;
  readonly options: EnvironmentVariableMutatorOptions;
}
